D:\a\tools.proto\tools.proto\runtime\src\message\util\string.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::message::{Error, FromBytes, Message, WriteTo}; |
30 | | use crate::util::ToUsize; |
31 | | use std::io::Write; |
32 | | use std::marker::PhantomData; |
33 | | |
34 | | pub struct NullTerminatedString; |
35 | | |
36 | | impl<'a> FromBytes<'a> for NullTerminatedString { |
37 | | type Output = &'a str; |
38 | | |
39 | 71 | fn from_bytes(slice: &'a [u8]) -> Result<Message<Self::Output>, Error> { |
40 | 71 | let string = slice |
41 | 71 | .iter() |
42 | 71 | .enumerate() |
43 | 541 | .find_map(|(id, v)| match *v == 0x0 { |
44 | 71 | true => Some(id), |
45 | 470 | false => None, |
46 | 541 | }) |
47 | 71 | .map(|pos| std::str::from_utf8(&slice[0..pos])) |
48 | 71 | .ok_or(Error::Truncated)?0 |
49 | 71 | .map_err(|_| Error::InvalidUtf80 )?0 ; |
50 | 71 | Ok(Message::new(string.len() + 1, string)) |
51 | 71 | } |
52 | | } |
53 | | |
54 | | impl WriteTo for NullTerminatedString { |
55 | | type Input<'a> = &'a str; |
56 | | |
57 | 46 | fn write_to<W: Write>(input: &Self::Input<'_>, mut out: W) -> Result<(), Error> { |
58 | 46 | out.write_all(input.as_bytes())?0 ; |
59 | 46 | out.write_all(&[0x0])?0 ; |
60 | 46 | Ok(()) |
61 | 46 | } |
62 | | } |
63 | | |
64 | | #[cfg(feature = "tokio")] |
65 | | impl crate::message::WriteToAsync for NullTerminatedString { |
66 | 13 | async fn write_to_async<W: tokio::io::AsyncWriteExt + Unpin>( |
67 | 13 | input: &Self::Input<'_>, |
68 | 13 | mut out: W, |
69 | 13 | ) -> crate::message::Result<()> { |
70 | 13 | out.write_all(input.as_bytes()).await?0 ; |
71 | 13 | out.write_all(&[0x0]).await?0 ; |
72 | 13 | Ok(()) |
73 | 13 | } |
74 | | } |
75 | | |
76 | | pub struct VarcharString<T>(PhantomData<T>); |
77 | | |
78 | | impl<'a, T: FromBytes<'a, Output: ToUsize>> FromBytes<'a> for VarcharString<T> { |
79 | | type Output = &'a str; |
80 | | |
81 | 2 | fn from_bytes(slice: &'a [u8]) -> Result<Message<Self::Output>, Error> { |
82 | 2 | let msg = T::from_bytes(slice)?0 ; |
83 | 2 | let size = msg.size(); |
84 | 2 | let subslice = &slice[size..size + msg.into_inner().to_usize()]; |
85 | 2 | let string = std::str::from_utf8(subslice).map_err(|_| Error::InvalidUtf80 )?0 ; |
86 | 2 | Ok(Message::new(size + string.len(), string)) |
87 | 2 | } |
88 | | } |
89 | | |
90 | | impl<T: WriteTo<Input<'static>: ToUsize>> WriteTo for VarcharString<T> { |
91 | | type Input<'a> = &'a str; |
92 | | |
93 | 2 | fn write_to<W: Write>(input: &Self::Input<'_>, mut out: W) -> Result<(), Error> { |
94 | 2 | T::write_to(&T::Input::from_usize(input.len()), &mut out)?0 ; |
95 | 2 | out.write_all(input.as_bytes())?0 ; |
96 | 2 | Ok(()) |
97 | 2 | } |
98 | | } |
99 | | |
100 | | #[cfg(feature = "tokio")] |
101 | | impl<T: crate::message::WriteToAsync<Input<'static>: ToUsize>> crate::message::WriteToAsync for VarcharString<T> { |
102 | 0 | async fn write_to_async<W: tokio::io::AsyncWriteExt + Unpin>( |
103 | 0 | input: &Self::Input<'_>, |
104 | 0 | mut out: W, |
105 | 0 | ) -> crate::message::Result<()> { |
106 | 0 | T::write_to_async(&T::Input::from_usize(input.len()), &mut out).await?; |
107 | 0 | out.write_all(input.as_bytes()).await?; |
108 | 0 | Ok(()) |
109 | 0 | } |
110 | | } |